import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {CommonTool} from '../../common/common.tool';
import {HTTP_URLS} from '../../base/base-http.url';

@Injectable({
  providedIn: 'root'
})
export class FileManagementService {

  constructor(private http: HttpClient,
              public commonTool: CommonTool) {

  }

  /**
   * 创建目录
   */
  public createFolder(req: any): Observable<Array<any>> {
    return this.http.post<Array<any>>(HTTP_URLS.createFolder, req);
  }

  /**
   * 查询目录集合
   */
  public getFolders(req: any): Observable<Array<any>> {
    return this.http.get<Array<any>>(this.commonTool.getParam(HTTP_URLS.getFolderAndFileList, req));
  }

  /**
   * 删除文件目录
   */
  public deleteFolderByIds(req: any): Observable<any> {
    return this.http.delete<any>(this.commonTool.getParam(HTTP_URLS.deleteFolderAndFileByIds, req));
  }

  /**
   * 分享文件
   */
  public createShareFile(req: { folderFiles; needCode; validTime }): Observable<any> {
    return this.http.post<any>(HTTP_URLS.createShareFile, req);
  }

  /**
   * 移动目录
   */
  public moveFileFolder(req): Observable<any> {
    return this.http.put<any>(HTTP_URLS.moveFileFolder, req);
  }

  /**
   * 复制目录
   */
  public copyFileFolder(req): Observable<any> {
    return this.http.put<any>(HTTP_URLS.copyFileFolder, req);
  }

  /**
   * 修改文件
   */
  public updateFile(id, req): Observable<any> {
    return this.http.put<any>(this.commonTool.getParam(HTTP_URLS.updateFile + id, req), {});
  }

  /**
   * 修改目录
   */
  public updateFolder(id, req): Observable<any> {
    return this.http.put<any>(this.commonTool.getParam(HTTP_URLS.updateFolder + id, req), {});
  }

  /**
   * 查询共享的临时文件
   *
   */
  public getShareFileList(): Observable<any> {
    return this.http.get(HTTP_URLS.getShareFileList);
  }


  /**
   * 删除共享文件
   */
  public deleteShareFile(req) {
    return this.http.delete<any>(HTTP_URLS.deleteShareFile + req);
  }

  /**
   * 下载文件
   */
  public downloadFile(id) {
    return this.http.get<any>(HTTP_URLS.getDownloadUrl + id);
  }

  /**
   * 分页查询链接
   */
  public getSafetyChainPaging(size, current, req) {
    const url = HTTP_URLS.getSafetyChainPaging + size + '/' + current;
    return this.http.get<any>(this.commonTool.getParam(url, req));
  }

  /**
   * 创建安全链接
   */
  public createSafetyChain(req) {
    return this.http.post<any>(HTTP_URLS.createSafetyChain, req);
  }

  /**
   * 更新安全链接
   */
  public updateSafetyChain(id, req) {
    return this.http.put<any>(HTTP_URLS.updateSafetyChain + id, req);
  }

  /**
   * 删除安全链接
   */
  public deleteSafetyChain(ids) {
    return this.http.delete<any>(HTTP_URLS.deleteSafetyChain + ids);
  }

}
